package com.java3_20;

public class Ticket {
    private int ticket;

    public Ticket() {

    }

    public Ticket(int ticket) {
        this.ticket = ticket;
    }

    public int getTicket() {
        return ticket;
    }

    public void setTicket(int ticket) {
        this.ticket = ticket;
    }

    public void addTicket() {
        synchronized (this) {
            while (ticket < 5) {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                ticket++;
                System.out.println("当前的票数为：" + ticket);
                this.notifyAll();
            }
            try {
                this.wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public void deleteTicket() {
        synchronized (this) {
            while(ticket>0){
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                ticket--;
                System.out.println("当前的票数为：" + ticket);
                this.notifyAll();
            }
            try {
                this.wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }

    }
}
